
package Acciones.Persistencia;

import Configuraciones.Configuraciones;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.swing.AbstractAction;
import javax.swing.JTable;
import javax.swing.KeyStroke;


public class AccionNuevo extends AbstractAction {
    private JTable tbl;
    private EntityManager em;
    private List ls;
    private Serializable o;
    public static String NOMBRE="Nuevo";
    public static String DESCRIPCION="Nuevo registro a la tabla.";
    protected static int KEVENT=KeyEvent.VK_N;
    
    public AccionNuevo() {
            putValue(NAME, NOMBRE);
            putValue(SHORT_DESCRIPTION, DESCRIPCION);
            putValue(MNEMONIC_KEY, new Integer(KEVENT));
            putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KEVENT, 
                    Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
            putValue(SMALL_ICON, Configuraciones.ICONO_NUEVO);
    }

    public AccionNuevo(JTable tbl, EntityManager em, List ls, Serializable o) {
        this();
        this.tbl = tbl;
        this.em = em;
        this.ls = ls;
        this.o=o;
    }
    
    

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            Serializable obj= getO().getClass().newInstance();
            em.persist(obj);
            ls.add(obj);
            int row =ls.size()-1;
            tbl.setRowSelectionInterval(row, row);
            tbl.scrollRectToVisible(tbl.getCellRect(row, 0, true));
            
        } catch (InstantiationException ex) {
            Logger.getLogger(AccionNuevo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(AccionNuevo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    public JTable getTbl() {
        return tbl;
    }


    public void setTbl(JTable tbl) {
        this.tbl = tbl;
    }

 
    public EntityManager getEm() {
        return em;
    }

  
    public void setEm(EntityManager em) {
        this.em = em;
    }


    public List getLs() {
        return ls;
    }


    public void setLs(List ls) {
        this.ls = ls;
    }


    public Serializable getO() {
        return o;
    }


    public void setO(Serializable o) {
        this.o = o;
    }
    
}
